package com.addplus.townmall.server.api.mapper.authority;

import com.addplus.townmall.server.api.model.authority.MenuElement;
import com.addplus.townmall.server.api.model.authority.ext.MenuRoleExt;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuElementMapper extends BaseAddMapper<MenuElement> {

    @Select("<script>\n" +
            "SELECT DISTINCT\n" +
            " m.e_id AS id \n" +
            "FROM\n" +
            " role_menuelement m\n" +
            " LEFT JOIN role r ON r.id = m.r_id \n" +
            " AND r.is_delete = 0\n" +
            " LEFT JOIN menu_element e ON e.id = m.e_id \n" +
            " AND e.is_delete = 0 \n" +
            "WHERE\n" +
            " m.is_delete = 0 AND e.type=#{type}\n" +
            " AND m.r_id IN\n" +
            " <foreach collection='roles' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach>\n" +
            "</script>")
    List<String> getUserFunctionList(@Param("roles") List<String> roles, @Param("type") Integer type);

    @Select("select e.id as id,CONCAT('/',e.method,f.url,e.name) as name from menu_element e left join menu_function f on f.id = e.m_id where f.is_delete= 0 and e.is_delete = 0")
    List<MenuElement> getMenuElementList();

    @Select("select e.id as id,CONCAT('/',e.method,f.url,e.name) as name from menu_element e left join menu_function f on f.id = e.m_id where f.is_delete= 0 and e.is_delete = 0 and e.m_id = #{mId}")
    List<MenuElement> getMenuElementListByfunctionId(@Param("mId") Integer mId);

    @Update("<script>\n" +
            "UPDATE menu_element SET is_delete = 1 WHERE\n" +
            "m_id IN\n" +
            "<foreach collection='ids' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach>\n" +
            "AND is_delete = 0\n" +
            "</script>")
    Integer updateMenuElementDelete(@Param("ids") List<Integer> mIdList);

    @Select("<script>\n" +
            "SELECT\n" +
            " me.id,\n" +
            " me.m_id,\n" +
            " me.name,\n" +
            " me.method\n" +
            "FROM\n" +
            " menu_element me\n" +
            " LEFT JOIN menu_function mf ON mf.id = me.m_id \n" +
            " AND mf.is_delete = 0\n" +
            " LEFT JOIN role_menuelement rm ON rm.e_id = me.id \n" +
            " AND rm.is_delete = 0 \n" +
            "WHERE\n" +
            " me.is_delete = 0 \n" +
            " AND mf.id = #{mId} AND rm.r_id IN\n" +
            "<foreach collection='roles' index='index' item='item' open='(' separator=',' close=')'>#{item}\n" +
            "</foreach>\n" +
            "</script>")
    List<MenuElement> getUserMenuElementList(@Param("roles") List<Integer> roleList, @Param("mId") Integer mId);

    @Select("SELECT\n" +
            " me.method AS url,\n" +
            " GROUP_CONCAT( DISTINCT ( r.id ) ) AS rId \n" +
            "FROM\n" +
            " role_menu_function rm\n" +
            " LEFT JOIN menu_function mf ON mf.id = rm.m_id \n" +
            " AND mf.is_delete = 0\n" +
            " LEFT JOIN role r ON r.id = rm.r_id \n" +
            " AND r.is_delete = 0\n" +
            " RIGHT JOIN menu_element me ON me.m_id = mf.id \n" +
            " AND me.is_delete = 0 \n" +
            "WHERE\n" +
            " rm.is_delete = 0 \n" +
            "GROUP BY\n" +
            " me.method")
    List<MenuRoleExt> getMunuElementGroupMethodRole(Page page);

    @Select("select count(1) as count from(SELECT max(mf.id) AS mId,me.method AS url,GROUP_CONCAT( DISTINCT ( r.id ) ) AS rId FROM role_menu_function rm LEFT JOIN menu_function mf ON mf.id = rm.m_id AND mf.is_delete = 0 LEFT JOIN role r ON r.id = rm.r_id AND r.is_delete = 0 RIGHT JOIN menu_element me ON me.m_id = mf.id AND me.is_delete = 0 WHERE rm.is_delete = 0 GROUP BY me.method) AS temp")
    Integer getMunuElementGroupMethodRoleByCount();

}